package com.ds.infra.mq.example.other;


import com.ds.infra.mq.core.api.MQConsumer;
import com.ds.infra.mq.core.constants.MQConstant;
import com.ds.infra.mq.example.Constant;

public class ConsumerExample2 {
    public static void main(String[] args) throws Exception {
        MQConsumer consumer = new MQConsumer("localhost", 9092, "test-consumer2");
        consumer.connect();

        // 添加合理的消费间隔
        int emptyCount = 0;
        while (true) {
            byte[] message = consumer.poll(Constant.TEST_TOPIC);
            if (message == null || MQConstant.EMPTY_STR.equals(new String(message))) {
                emptyCount++;
                if (emptyCount % 20 == 0) {
                    System.out.println("Waiting for messages...");
                }
                Thread.sleep(1000); // 添加等待时间
                continue;
            }
            emptyCount = 0;
            System.out.println("Received: " + new String(message));
        }
    }
}
